<template>
  <el-container class="home-container">
    <el-header>
      <img src="../assets/heima.png" alt="" />
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened
          :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <!-- <i class="el-icon-location"></i> -->
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+ child.path" v-for="child in item.children" :key="child.id"
              @click="saveNavState('/'+ child.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{child.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main><router-view /></el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        activePath: '',
        isCollapse: false,
        menuList: [],
        iconsObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        }
      }
    },
    created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },

    methods: {
      saveNavState(activePath) {
        // console.log(this.activePath);
        this.activePath = activePath
        window.sessionStorage.setItem('activePath', activePath)
      },
      toggleCollapse() {
        this.isCollapse = !this.isCollapse

      },
      async getMenuList() {
        const {
          data: res
        } = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menuList = res.data
        // console.log(res)
      },
      logout() {
        window.sessionStorage.clear()
        this.$router.push("/login")
      }
    }
  }
</script>

<style lang="less" scoped>

  .el-menu {
    border-right: 0;
  }

  i {
    margin-right: 5px;
  }

  .toggle-button {
    cursor: pointer;
    background-color: #4A5064;
    color: #fff;
    font-size: 10px;
    text-align: center;
    line-height: 24px;
  }

  .el-header {
    display: flex;
    justify-content: space-between;
    background-color: #373d41;
    color: #333;
    align-items: center;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #333744;
    color: #333;
    line-height: 200px;
  }

  .el-main {
    background-color: #eaedf1;
    color: #333;
  }

  .el-container {
    height: 100%;
  }


  body>.el-container {
    margin-bottom: 40px;
  }


  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
</style>
